require 'base64'
require 'openssl'
require 'digest'
require 'securerandom'

class CompanyApi < Grape::API
	#通用接口
	version 'v1', using: :path
	format :json
	default_format :json
	formatter :json, Grape::Formatter::Rabl
	resource :company do

		desc '注册接口'
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :company_name, type: String, desc: '公司名称'
			optional :passwd, type: String, desc: '密码'
			optional :confirm_passwd, type: String, desc: '密码确认'
			optional :phone, type: String, desc: '联系电话'
		end
		post 'register' do
			content_type 'application/x-www-form-urlencoded'
			if params[:user_name].nil? || params[:company_name].nil? || params[:passwd].nil? || params[:confirm_passwd].nil? || params[:phone].nil?
				puts params[:user_name] 
				puts  params[:company_name]
				{code: '400', msg: '缺少参数'}
			elsif !(params[:passwd] == params[:confirm_passwd])
				{code: '400', msg: '密码和确认密码不一致'}
			else
				company_yz = Company.where(user_name: params[:user_name])
				if company_yz.empty?
					company = Company.new
					passwd = CommonApi::des_decrypt(params[:passwd])
					company.passwd = Digest::MD5.hexdigest(passwd)
					company.user_name = params[:user_name]
					company.company_name = params[:company_name]
					company.phone = params[:phone]
					company.approvl_status = 0
					company.auth_deadline = Time.now + 86400*7
					company.save
					{
						code: '0',
						msg: 'OK',
						company_id: company.id
					}
				else
					{code: '400', msg: '用户名重复'}
				end				
			end
		end
		desc '登录功能'
		params do
			optional :user_name, type: String, desc: '登录用户名(手机号/身份证号)'
			optional :passwd, type: String, desc: '密码'
		end
		post 'login' do
			content_type 'application/x-www-form-urlencoded'
			if params[:user_name].nil?
				{code: '400', msg: '请输入企业用户名'}
			elsif params[:passwd].nil?
				{code: '400', msg: '请输入密码'}
			else
				passwd = Digest::MD5.hexdigest(CommonApi::des_decrypt(params[:passwd]))
				company = Company.where(user_name: params[:user_name], passwd: passwd, publish_status: 1).first
				if company.nil?
					{code: '400', msg: '用户名或密码错误'}
				elsif company.approvl_status == 2
					{code: '400', msg: '用户审核不通过'}
				else
					{
						code: '0',
						msg: 'OK',
						company_info:{
							'company_id': company.id,
							'company_name': company.company_name,
							'approvl_status': company.approvl_status,
							'phone': company.phone,
							"auth_deadline": company.auth_deadline.to_time.strftime('%Y-%m-%d'),
						}
					}
				end	
			end
		end

		desc '企业信息上传功能'
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :register_type, type: String, desc: '企业注册类型'
			optional :company_name, type: String, desc: '企业注册类型'
			optional :credit_code, type: String, desc: '统一社会编码'
			optional :register_address, type: String, desc: '注册地'
			optional :company_address, type: String, desc: '办公地址'
			optional :register_time, type: String, desc: '注册时间'
			optional :business_licence_url, type: String, desc: '营业执照上传地址'
		end
		post 'information_upload' do
			content_type 'application/x-www-form-urlencoded'
			if params[:user_name].nil?
				{code: '400', msg: '请输入企业用户名'}
			elsif params[:passwd].nil?
				{code: '400', msg: '请输入密码'}
			elsif params[:register_type].nil?
				{code: '400', msg: '企业注册类型为空'}
			elsif params[:company_name].nil?
				{code: '400', msg: '企业名称为空'}
			elsif params[:credit_code].nil?
				{code: '400', msg: '统一社会编码为空'}
			elsif params[:register_address].nil?
				{code: '400', msg: '注册地为空'}
			elsif params[:company_address].nil?
				{code: '400', msg: '办公地址为空'}
			elsif params[:register_time].nil?
				{code: '400', msg: '注册时间为空'}
			elsif params[:business_licence_url].nil?
				{code: '400', msg: '请上传营业执照'}
			else
				passwd = Digest::MD5.hexdigest(CommonApi::des_decrypt(params[:passwd]))
				company = Company.where(user_name: params[:user_name], passwd: passwd).first
				if company.nil?
					{code: '400', msg: '用户名或密码错误'}
				else
					company.register_type = params[:register_type]
					company.credit_code = params[:credit_code]
					company.company_name = params[:company_name]
					company.register_address = params[:register_address]
					company.company_address = params[:company_address]
					company.register_time = params[:register_time]
					company.business_licence_url = params[:business_licence_url]
					company.business_licence_url = params[:business_licence_url]
					company.approvl_status = 3
					company.save
					{
						code: '0',
						msg: 'OK',
						approvl_status: company.approvl_status
					}
				end	
			end
		end
		desc '企业用户查询'
		params do
			optional :company_id, type: String, desc: '企业ID'
		end
		get 'company' do
			content_type 'application/x-www-form-urlencoded'
			if params[:company_id].nil?
				{code: '400', msg: '缺少参数'}
			else
				company = Company.find(params[:company_id])
				if company.nil?
					{code: '400', msg: '未查询到该企业'}
				else
					project_num = Project.where(certification_company: params[:company_id], certification_status: 1).count
					{
						code: '0',
						msg: 'OK',
						company_info:{
							'company_id': company.id,
							'company_level': company.company_level,
							'company_desc': company.company_desc,
							'company_name': company.company_name,
							'approvl_status': company.approvl_status,
							'phone': company.phone,
							"auth_deadline": company.auth_deadline.to_time.strftime('%Y-%m-%d'),
							"register_type": company.register_type,
							"credit_code":  company.credit_code,
							"register_address": company.register_address.nil? ? "" : CommonApi::cover_city(company.register_address),
							"company_address": company.company_address,
							"register_time": company.register_time.nil? ? "" : CommonApi::format_time(company.register_time),
							"business_licence_url": company.business_licence_url,
							"project_num": project_num,
						}
					}
				end	
			end
		end

		desc '按照名称查询企业列表'
		params do
			optional :company_name, type: String, desc: '企业名称'
		end
		get 'companies' do
			content_type 'application/x-www-form-urlencoded'
			if params[:company_name].nil?
				{code: '400', msg: '缺少参数'}
			else
				companies = Company.where("company_name LIKE ? and approvl_status = 1", "%"+params[:company_name]+"%")
				company_list = []
				companies.each do |coms|
					company =  Hash.new 
					company["company_name"] = coms.company_name
					company["company_id"] = coms.id
					company_list << company
				end
				{code: "0", msg: "OK", company_list: company_list}
			end
		end

		desc '用户查询企业认证状态'
		params do
			optional :user_id, type: String, desc: '用户ID'
		end
		get 'user_company' do
			content_type 'application/x-www-form-urlencoded'
			if params[:user_id].nil?
				{code: '400', msg: '缺少参数'}
			else
				user_detail = UserDetail.where(user_id: params[:user_id]).first
				if user_detail.company_id.nil?
					company_name = ""
					company_id = 0
				else
					company = Company.find(user_detail.company_id)
					company_name = company.company_name
					company_id = company.id
				end
				{code: "0", msg: "OK", company_id: company_id, company_name: company_name, company_validate_type: user_detail.company_validate_type}
			end
		end

		desc '用户申请企业认证'
		params do
			optional :user_id, type: String, desc: '用户ID'
			optional :company_id, type: String, desc: '公司ID'
		end
		post 'user_apply_company' do

			content_type 'application/x-www-form-urlencoded'
			if params[:user_id].nil? || params[:company_id].nil?
				{code: '400', msg: '缺少参数'}
			else
				company = Company.find(params[:company_id])
				user_detail = UserDetail.where(user_id: params[:user_id]).first
				user_detail.company_id = params[:company_id]
				user_detail.company_validate_type = 1
				user_detail.save

				{code: "0", msg: "OK", company_name: company.company_name, company_validate_type: user_detail.company_validate_type}
			end
		end
		desc '企业处理人员申请'
		params do
			optional :user_id, type: String, desc: '用户ID'
			optional :company_id, type: String, desc: '公司ID'
			optional :dispose_status, type: String, desc: '处理状态'
		end
		post 'dispose_company_user' do

			content_type 'application/x-www-form-urlencoded'
			if params[:user_id].nil? || params[:company_id].nil?
				{code: '400', msg: '缺少参数'}
			else
				user_detail = UserDetail.where("user_id = ? and company_id = ? ",params[:user_id], params[:company_id]).first
				if user_detail.nil?
					{code: '400', msg: '未查询到该申请'}
				else
					user_detail.company_validate_type = params[:dispose_status]
					user_detail.save
					{code: "0", msg: "OK", company_validate_type: params[:dispose_status]}
				end
				

				{code: "0", msg: "OK",  company_validate_type: user_detail.company_validate_type}
			end
		end
		desc '企业查询用户申请列表'
		params do
			optional :company_id, type: String, desc: '公司ID'
			optional :page, type:String, desc: '页码'
		end
		get 'company_user' do

			content_type 'application/x-www-form-urlencoded'
			if params[:company_id].nil? 
				{code: '400', msg: '缺少参数'}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				user_details = UserDetail.where(company_id: params[:company_id], company_validate_type: 1).paginate(:page => page)
				user_list = []
					user_details.each do |ud|
						user_detail_info = Hash.new
						user = User.find(ud.user_id)
						user_detail_info["qi_content"] =  user.name + "申请企业认证" 
						user_detail_info["user_id"] = user.id
						user_list << user_detail_info
					end
				count = user_details.size
				next_page = params[:page].to_i + 1
				{code: '0', msg: 'OK', "user_list": user_list, count: count, next_page: next_page}
			end
		end
	end
end
